﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FisheryPlatform.Entity;
using FisheryPlatform.DataAccess.Option;

namespace FisheryPlatform.Bussiness.Option
{
    public class OptionManager
    {
        public static bool Save(OptionsEntity obj)
        {
            return OptionsDB.Save(obj);
        }
                
        public static bool Save(string name, string value, bool autoLoad, bool isAdd)
        {
            #region
            var optionInfo = GetByOptionName(name);
            var optionValues = "";
            var optionValueLst = optionInfo.Value.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList<string>();
            if (optionValueLst.Contains(value))
            {
                if (isAdd == false)
                {
                    optionValueLst.Remove(value);
                }
            }
            else
            {
                if (isAdd)
                {
                    optionValueLst.Add(value);
                }
            }
            optionValues = string.Join(",", optionValueLst.ToArray());
            if (optionValues != optionInfo.Value)
            {
                optionInfo.Name = name;//防止获取不到
                optionInfo.Value = optionValues;
                optionInfo.AutoLoad = (byte)(autoLoad ? 1 : 0);
                return OptionManager.Save(optionInfo);
            }
            return true;
            #endregion
        }

        /// <summary>
        /// 默认 autoLoad=true
        /// </summary>
        /// <returns></returns>
        public static bool Save(string name, string value, bool isAdd)
        {
            return Save(name, value, true, isAdd);
        }

        public static OptionsEntity GetByOptionName(string name)
        {
            return OptionsDB.GetByOptionName(name);
        }

        public static List<OptionsEntity> GetAll()
        {
            return OptionsDB.GetAll();
        }
    }
}
